package com.xdog.okx.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

/**
 * OKX API签名测试工具类
 * 用于测试和诊断签名相关问题
 */
@Slf4j
@Component
public class OkxSignatureTestUtil {

    @Resource
    private OkxApiUtil okxApiUtil;

    /**
     * 测试时间戳差异对签名的影响
     * 通过快速连续生成多个签名来检查时间戳是否可能导致签名错误
     */
    public void testTimestampConsistency() {
        log.info("开始测试时间戳一致性...");
        
        String method = "GET";
        String requestPath = "/api/v5/xlayer/token/token-list";
        String body = "";
        
        // 快速生成多个签名进行测试
        for (int i = 0; i < 5; i++) {
            String timestamp1 = OkxSignatureUtils.generateTimestamp();
            String signature1 = OkxSignatureUtils.generateSignature(
                timestamp1, 
                method, 
                requestPath, 
                body, 
                "test_secret_key"
            );
            
            String timestamp2 = OkxSignatureUtils.generateTimestamp();
            String signature2 = OkxSignatureUtils.generateSignature(
                timestamp2, 
                method, 
                requestPath, 
                body, 
                "test_secret_key"
            );
            
            log.info("测试 {} - 时间戳1: {}, 签名1: {}", i+1, timestamp1, signature1);
            log.info("测试 {} - 时间戳2: {}, 签名2: {}", i+1, timestamp2, signature2);
            
            if (!timestamp1.equals(timestamp2)) {
                log.warn("时间戳不一致: {} vs {}", timestamp1, timestamp2);
            }
            
            if (!signature1.equals(signature2)) {
                log.warn("签名不一致，可能是由于时间戳差异导致");
            }
            
            // 短暂休眠
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
        }
        
        log.info("时间戳一致性测试完成");
    }
    
    /**
     * 验证签名生成过程中的各个步骤
     */
    public void debugSignatureGeneration(String apiKey, String secretKey, String passphrase, 
                                       String method, String requestPath, String body) {
        log.info("开始调试签名生成过程...");
        log.info("参数 - method: {}, requestPath: {}, body: {}", method, requestPath, body);
        
        // 生成时间戳
        String timestamp = OkxSignatureUtils.generateTimestamp();
        log.info("生成的时间戳: {}", timestamp);
        
        // 生成签名
        String signature = OkxSignatureUtils.generateSignature(timestamp, method, requestPath, body, secretKey);
        log.info("生成的签名: {}", signature);
        
        // 生成完整的请求头
        Map<String, String> headers = OkxSignatureUtils.generateAuthHeaders(
            apiKey, secretKey, passphrase, method, requestPath, body);
        
        log.info("生成的请求头:");
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            if ("OK-ACCESS-SIGN".equals(entry.getKey())) {
                log.info("  {}: [签名已隐藏]", entry.getKey());
            } else {
                log.info("  {}: {}", entry.getKey(), entry.getValue());
            }
        }
        
        log.info("签名生成过程调试完成");
    }
}